package com.ruoyi.develop.lvRukumingxi.utils;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProductProcess.service.impl.FactoryProductProcessServiceImpl;
import com.ruoyi.develop.factory.factoryProductStore.service.impl.FactoryProductStoreServiceImpl;
import com.ruoyi.develop.factory.factoryTestMachineryHammer.service.impl.FactoryTestMachineryHammerServiceImpl;
import com.ruoyi.develop.factory.factoryTestMachineryLink.service.impl.FactoryTestMachineryLinkServiceImpl;
import com.ruoyi.develop.factory.factoryTestMachineryStick.service.impl.FactoryTestMachineryStickServiceImpl;
import com.ruoyi.develop.factory.factoryTestSample.service.impl.FactoryTestSampleServiceImpl;
import com.ruoyi.develop.factory.factoryTestSize.service.impl.FactoryTestSizeServiceImpl;
import com.ruoyi.develop.factory.factoryTestSurface.domain.FactoryTestSurface;
import com.ruoyi.develop.factory.factoryTestSurface.service.impl.FactoryTestSurfaceServiceImpl;
import com.ruoyi.develop.factory.factoryTestZn.service.impl.FactoryTestZnServiceImpl;
import com.ruoyi.develop.finance.financeAp.service.IFinanceApService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.impl.GoodsOrderInServiceImpl;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.service.IGoodsSaledoService;
import com.ruoyi.develop.goodsSaledo.service.impl.GoodsSaledoServiceImpl;
import com.ruoyi.develop.lvDaohuo.domain.LvDaohuo;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import com.ruoyi.develop.lvDaohuoDetail.service.ILvDaohuoDetailService;
import com.ruoyi.develop.lvRuku.domain.LvRuku;
import com.ruoyi.develop.lvRuku.mapper.LvRukuMapper;
import com.ruoyi.develop.lvRuku.service.impl.LvRukuServiceImpl;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import com.ruoyi.develop.lvRukumingxi.service.impl.LvRukumingxiServiceImpl;
import com.ruoyi.develop.produceRecord.domain.ProduceRecord;
import com.ruoyi.develop.produceRecordDetail.domain.ProduceRecordDetail;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: lmm
 * @Date: 2024/3/28 11:20
 */
@Component
@Configuration
public class RukumingxiUtils {

    @Resource
    private ISysDeptService sysDeptService;
    @Resource
    private ILvDaohuoDetailService lvDaohuoDetailService;
    @Resource
    private GoodsOrderInServiceImpl goodsOrderInService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Resource
    private LvRukumingxiServiceImpl lvRukumingxiService;
    @Resource
    private LvRukuServiceImpl lvRukuService;
    @Resource
    private GoodsSaledoServiceImpl goodsSaledoService;
    @Resource
    private LvRukuMapper lvRukuMapper;
    @Resource
    private FactoryTestZnServiceImpl factoryTestZnService;
    @Resource
    private FactoryTestSampleServiceImpl factoryTestSampleService;
    @Resource
    private FactoryTestSurfaceServiceImpl factoryTestSurfaceService;
    @Resource
    private FactoryTestSizeServiceImpl factoryTestSizeService;
    @Resource
    private FactoryTestMachineryHammerServiceImpl factoryTestMachineryHammerService;
    @Resource
    private FactoryTestMachineryLinkServiceImpl factoryTestMachineryLinkService;
    @Resource
    private FactoryTestMachineryStickServiceImpl factoryTestMachineryStickService;
    @Resource
    private FactoryProductProcessServiceImpl factoryProductProcessService;
    @Resource
    private FactoryProductStoreServiceImpl factoryProductStoreService;

    @Autowired
    private IFinanceApService financeApService;

    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;



    /** 根据到货 入库 */
    public void rukuByDaohuoAndDaohuoDetail(LvDaohuo lvDaohuo,LvDaohuoDetail entity,LvRuku lvRuku) {
        entity.setStorageDate(DateUtils.getNowDate());
        entity.setStorageState(BusinessConstants.STORAGE_STATE_YES);
        // 更新库存
        GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(entity.getGoodId());
        if(BusinessConstants.GOODS_ARRIVAL_STATUS_RUKU.equals(entity.getTuihuoType())){
            goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setDun(goodsOrderIn.getDun().add(entity.getDaohuoDun()));
            GoodsSale goodsSale=goodsSaleService.selectById(entity.getGoodsSaleId());
            if(goodsSale!=null){
                goodsSale.setStatus(BusinessConstants.SALE_TYPE_9);
                goodsSaleService.update(goodsSale);
            }
        }else{
            goodsOrderIn.setCount(goodsOrderIn.getCount().add(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(entity.getDaohuoCount()));
        }
        goodsOrderInService.update(goodsOrderIn);

        // 更新入库明细
        LvRukumingxi rukuMingxi = new LvRukumingxi();
        rukuMingxi.setGoodsId(entity.getGoodId());
        rukuMingxi.setProjectId(entity.getProjectId());
        rukuMingxi.setProjectNo(entity.getProjectNo());
        rukuMingxi.setGoodsSaleId(entity.getGoodsSaleId());

        if(entity.getTaxRate()!=null){
            rukuMingxi.setTaxRate(entity.getTaxRate());
            rukuMingxi.setTotalPriceNoTax(entity.getTotalPriceNoTax());
            rukuMingxi.setUnitPriceNoTax(entity.getUnitPriceNoTax());
            rukuMingxi.setSingleWeight(entity.getSingleWeight());
        }

        rukuMingxi.setDaohuoCount(entity.getDaohuoCount());//入库数量
        rukuMingxi.setDun(entity.getDaohuoDun());
        rukuMingxi.setCanCount(entity.getDaohuoCount());
        rukuMingxi.setRukudate(DateUtils.getNowDate());
        rukuMingxi.setType(BusinessConstants.TYPE_RUKU);
        rukuMingxi.setIsdo(BusinessConstants.STATUS_NO);
        rukuMingxi.setDaohuoId(entity.getDaohuoId());
        rukuMingxi.setDaohuono(entity.getNos());
        rukuMingxi.setGoodsBuyPrice(entity.getUnitPrice());//入库单价
        rukuMingxi.setGoodsBuyTotal(entity.getTotalPrice());//入库总价
        rukuMingxi.setId(IdUtils.fastSimpleUUID());
        rukuMingxi.setCreateTime(DateUtils.getNowDate());
        rukuMingxi.setRukuren(SecurityUtils.getNickName());
        rukuMingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
        rukuMingxi.setCode(goodsOrderIn.getCode());
        rukuMingxi.setUnit(goodsOrderIn.getUnit());
        rukuMingxi.setSupplierId(entity.getSupplierId());
        rukuMingxi.setSupplierName(entity.getSupplierName());
        rukuMingxi.setDaohuodetailId(entity.getId());
        rukuMingxi.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
        rukuMingxi.setParentId(lvRuku.getId());
        rukuMingxi.setNos(lvRuku.getNos());
        rukuMingxi.setRukuno(lvRuku.getNos());
        rukuMingxi.setCaigouId(lvDaohuo.getBuyId());
        rukuMingxi.setCaigouno(lvDaohuo.getBuyNo());
        rukuMingxi.setDaohuono(lvDaohuo.getNos());
        LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
        rukuMingxi.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().add(entity.getTotalPrice()));
        lvRukumingxiService.insert(rukuMingxi);
        lvDaohuoDetailService.update(entity);

        updateAllTestRukumingxiByDaohuo(entity.getId(),rukuMingxi.getId());

        // 更新价格
        try {
            LvRukumingxi rukuMingxiSelect = new LvRukumingxi();
            rukuMingxiSelect.setGoodsId(entity.getGoodId());
            rukuMingxiSelect.setType(BusinessConstants.TYPE_RUKU);
            rukuMingxiSelect.setIsdo(BusinessConstants.STATUS_NO);
            rukuMingxiSelect.setStatus(BusinessConstants.STORAGE_STATE_YES);
            rukuMingxiSelect.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
            List<LvRukumingxi> rukuMingxiCaigouList = lvRukumingxiService.selectList(rukuMingxiSelect);
            BigDecimal avgPrice=BigDecimal.ZERO;//
            if(rukuMingxiCaigouList.size()>0){
                BigDecimal goodsBuyPrice=BigDecimal.ZERO;//单价
                int i=0;
                for(LvRukumingxi rukuMingxiCaigou:rukuMingxiCaigouList){
                    if(rukuMingxiCaigou.getGoodsBuyPrice().compareTo(BigDecimal.ZERO)>0 && rukuMingxiCaigou.getDaohuoCount().compareTo(BigDecimal.ZERO)>0 ){
                        goodsBuyPrice=goodsBuyPrice.add(rukuMingxiCaigou.getGoodsBuyPrice());
                        i = i+1;
                    }
                }
                System.out.print(i+"   goodsBuyPrice:::  "+goodsBuyPrice);
                if(goodsBuyPrice.compareTo(BigDecimal.ZERO)>0 && i>0 ){
                    avgPrice=goodsBuyPrice.divide(BigDecimal.valueOf(i),4, RoundingMode.HALF_UP);
                }
            }
            goodsOrderIn.setCostPrice(avgPrice);
            goodsOrderIn.setCost(avgPrice.multiply(goodsOrderIn.getCount()));
            goodsOrderInService.update(goodsOrderIn);
        } catch (Exception e) {
            System.out.println("平均价格计算错误");
            e.printStackTrace();
        }

    }







    /**
     * 根据货物明细入库
     * @param
     * @return
     */
    public void rukuByDaohuoDetail(LvDaohuoDetail entity,LvRuku lvRuku) {
        entity.setStorageDate(DateUtils.getNowDate());
        entity.setStorageState(BusinessConstants.STORAGE_STATE_YES);
        // 更新库存
        GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(entity.getGoodId());
        if(BusinessConstants.GOODS_ARRIVAL_STATUS_RUKU.equals(entity.getTuihuoType())){
            goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setDun(goodsOrderIn.getDun().add(entity.getDaohuoDun()));
            GoodsSale goodsSale=goodsSaleService.selectById(entity.getGoodsSaleId());
            if(goodsSale!=null){
                goodsSale.setStatus(BusinessConstants.SALE_TYPE_9);
                goodsSaleService.update(goodsSale);
            }
        }else{
            goodsOrderIn.setCount(goodsOrderIn.getCount().add(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(entity.getDaohuoCount()));
        }
        goodsOrderInService.update(goodsOrderIn);

        // 更新入库明细
        LvRukumingxi rukuMingxi = new LvRukumingxi();
        rukuMingxi.setGoodsId(entity.getGoodId());
        rukuMingxi.setProjectId(entity.getProjectId());
        rukuMingxi.setProjectNo(entity.getProjectNo());
        rukuMingxi.setGoodsSaleId(entity.getGoodsSaleId());
        rukuMingxi.setDaohuoCount(entity.getDaohuoCount());//入库数量
        rukuMingxi.setDun(entity.getDaohuoDun());
        rukuMingxi.setCanCount(entity.getDaohuoCount());
        rukuMingxi.setRukudate(DateUtils.getNowDate());
        rukuMingxi.setType(BusinessConstants.TYPE_RUKU);
        rukuMingxi.setIsdo(BusinessConstants.STATUS_NO);
        rukuMingxi.setDaohuoId(entity.getDaohuoId());
        rukuMingxi.setDaohuono(entity.getNos());
        rukuMingxi.setGoodsBuyPrice(entity.getUnitPrice());//入库单价
        rukuMingxi.setGoodsBuyTotal(entity.getTotalPrice());//入库总价
        rukuMingxi.setId(IdUtils.fastSimpleUUID());
        rukuMingxi.setCreateTime(DateUtils.getNowDate());
        rukuMingxi.setRukuren(SecurityUtils.getNickName());
        rukuMingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
        rukuMingxi.setCode(goodsOrderIn.getCode());
        rukuMingxi.setUnit(goodsOrderIn.getUnit());
        rukuMingxi.setSupplierId(entity.getSupplierId());
        rukuMingxi.setSupplierName(entity.getSupplierName());
        rukuMingxi.setDaohuodetailId(entity.getId());
        rukuMingxi.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
        rukuMingxi.setParentId(lvRuku.getId());
        rukuMingxi.setNos(lvRuku.getNos());
        rukuMingxi.setRukuno(lvRuku.getNos());
        //rukuMingxi.setMainId();
        //rukuMingxi.setCaigouno();
        lvRukumingxiService.insert(rukuMingxi);
        lvDaohuoDetailService.update(entity);

        updateAllTestRukumingxiByDaohuo(entity.getId(),rukuMingxi.getId());

        // 更新价格
        try {
            LvRukumingxi rukuMingxiSelect = new LvRukumingxi();
            rukuMingxiSelect.setGoodsId(entity.getGoodId());
            rukuMingxiSelect.setType(BusinessConstants.TYPE_RUKU);
            rukuMingxiSelect.setIsdo(BusinessConstants.STATUS_NO);
            rukuMingxiSelect.setStatus(BusinessConstants.STORAGE_STATE_YES);
            rukuMingxiSelect.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
            List<LvRukumingxi> rukuMingxiCaigouList = lvRukumingxiService.selectList(rukuMingxiSelect);
            BigDecimal avgPrice=BigDecimal.ZERO;//
            if(rukuMingxiCaigouList.size()>0){
                BigDecimal goodsBuyPrice=BigDecimal.ZERO;//单价
                int i=0;
                for(LvRukumingxi rukuMingxiCaigou:rukuMingxiCaigouList){
                    if(rukuMingxiCaigou.getGoodsBuyPrice().compareTo(BigDecimal.ZERO)>0 && rukuMingxiCaigou.getDaohuoCount().compareTo(BigDecimal.ZERO)>0 ){
                        goodsBuyPrice=goodsBuyPrice.add(rukuMingxiCaigou.getGoodsBuyPrice());
                        i = i+1;
                    }
                }
                System.out.print(i+"   goodsBuyPrice:::  "+goodsBuyPrice);
                if(goodsBuyPrice.compareTo(BigDecimal.ZERO)>0 && i>0 ){
                    avgPrice=goodsBuyPrice.divide(BigDecimal.valueOf(i),4, RoundingMode.HALF_UP);
                }
            }
            goodsOrderIn.setCostPrice(avgPrice);
            goodsOrderIn.setCost(avgPrice.multiply(goodsOrderIn.getCount()));
            goodsOrderInService.update(goodsOrderIn);
        } catch (Exception e) {
            System.out.println("平均价格计算错误");
            e.printStackTrace();
        }

    }

    /**
     * 所有质检 根据 到货id 更新入库明细id明细入库
     */
    private void updateAllTestRukumingxiByDaohuo(String daohuoDetailId, String rukuMingxiId) {
        factoryTestZnService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestSampleService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestSurfaceService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestSizeService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestMachineryHammerService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestMachineryLinkService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryTestMachineryStickService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryProductProcessService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
        factoryProductStoreService.updateRukumingxiByDaohuo(daohuoDetailId,rukuMingxiId);
    }


    /**
     * 根据货物明细出库
     * @param
     * @return
     */
    public void chukuByDaohuoDetail(LvRuku lvRuku,LvDaohuoDetail entity) {
        entity.setStorageDate(DateUtils.getNowDate());
        entity.setStorageState(BusinessConstants.STORAGE_STATE_UN);
        // 更新库存
        GoodsOrderIn goodsOrderIn = goodsOrderInService.selectById(entity.getGoodId());
        if(BusinessConstants.GOODS_ARRIVAL_STATUS_RUKU.equals(entity.getTuihuoType())){
            goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
            GoodsSale goodsSale=goodsSaleService.selectById(entity.getGoodsSaleId());
            if(goodsSale!=null){
                goodsSale.setStatus(BusinessConstants.SALE_TYPE_9);
                goodsSaleService.update(goodsSale);
            }
        }else{
            goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getDaohuoCount()));
            goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getDaohuoCount()));
        }
        goodsOrderInService.update(goodsOrderIn);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukumingxiService.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }

        // 更新入库明细
        LvRukumingxi rukuMingxi = new LvRukumingxi();
        rukuMingxi.setParentId(lvRuku.getId());
        rukuMingxi.setNos(lvRuku.getNos());
        rukuMingxi.setRukuno(lvRuku.getNos());
        rukuMingxi.setGoodsId(entity.getGoodId());
        rukuMingxi.setProjectId(entity.getProjectId());
        rukuMingxi.setProjectNo(entity.getProjectNo());
        rukuMingxi.setGoodsSaleId(entity.getGoodsSaleId());
        rukuMingxi.setDaohuoCount(entity.getDaohuoCount());
        rukuMingxi.setCanCount(entity.getDaohuoCount());
        rukuMingxi.setRukudate(DateUtils.getNowDate());
        rukuMingxi.setType(BusinessConstants.TYPR_CHUKU);
        rukuMingxi.setIsdo(BusinessConstants.STATUS_NO);
        rukuMingxi.setDaohuoId(entity.getDaohuoId());
        rukuMingxi.setGoodsBuyPrice(entity.getUnitPrice());
        rukuMingxi.setGoodsBuyTotal(entity.getTotalPrice());
        rukuMingxi.setId(IdUtils.fastSimpleUUID());
        rukuMingxi.setCreateTime(DateUtils.getNowDate());
        rukuMingxi.setRukuren(SecurityUtils.getNickName());
        rukuMingxi.setStatus(BusinessConstants.STORAGE_STATE_YES);
        rukuMingxi.setNos(codeMaxString);
        rukuMingxi.setRukuno(codeMaxString);
        rukuMingxi.setCode(goodsOrderIn.getCode());
        rukuMingxi.setUnit(goodsOrderIn.getUnit());
        rukuMingxi.setSupplierId(entity.getSupplierId());
        rukuMingxi.setSupplierName(entity.getSupplierName());
        rukuMingxi.setDaohuodetailId(entity.getId());
        rukuMingxi.setDaohuono(entity.getNos());
        rukuMingxi.setIsZhijian(BusinessConstants.INSPECT_STATE_YES);
        LvRukumingxi lvRukumingxiBeginMoney=lvRukumingxiMapper.selectBeginMoney();
        rukuMingxi.setBeginMoney(lvRukumingxiBeginMoney.getBeginMoney().subtract(entity.getTotalPrice()));
        lvRukumingxiService.insert(rukuMingxi);
        lvDaohuoDetailService.update(entity);
    }

    /**
     * 根据到货入库
     * @param
     * @return
     */
    public void rukuByDaohuo(LvDaohuo lvDaohuo) {
        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(lvDaohuo.getId());
        List<LvDaohuoDetail> lvDaohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukumingxiService.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }
        LvRuku lvRuku=new LvRuku();
        lvRuku.setNos(codeMaxString);
        lvRuku.setState(BusinessConstants.SALE_TYPE_9);
        lvRuku.setCreateBy(SecurityUtils.getUsername());
        lvRuku.setCreateTime(DateUtils.getNowDate());
        lvRuku.setRukuDate(new Date());
        lvRuku.setRukuid(SecurityUtils.getUserId());
        lvRuku.setRukupeople(SecurityUtils.getUsername());
        lvRukuMapper.insert(lvRuku);

        for (LvDaohuoDetail daohuoDetail : lvDaohuoDetailList) {
            rukuByDaohuoDetail(daohuoDetail,lvRuku);
        }
    }


    /**
     * 根据到货出库
     * @param
     * @return
     */
    public void chukuByDaohuo(LvDaohuo lvDaohuo) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukuMapper.selectMaxCode(today);
        if(StringUtils.isNotBlank(codeMaxString)){
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString)+1+"";
        }else{
            codeMaxString = today+"0001";
        }
        LvRuku lvRuku=new LvRuku();
        lvRuku.setNos(codeMaxString);
        lvRuku.setCreateBy(SecurityUtils.getUsername());
        lvRuku.setCreateTime(DateUtils.getNowDate());
        lvRuku.setRukuDate(new Date());
        lvRuku.setRukuid(SecurityUtils.getUserId());
        lvRuku.setRukupeople(SecurityUtils.getUsername());
        lvRuku.setType(BusinessConstants.TYPR_CHUKU);
        lvRuku.setSource(BusinessConstants.RUKU_SOURCE_BUY);
        lvRuku.setDaohuoId(lvDaohuo.getId());
        lvRuku.setDaohuoNos(lvDaohuo.getNos());
        lvDaohuo.setYwType(lvDaohuo.getYwType());
        lvRuku.setSupplierName(lvDaohuo.getSupplierName());
        lvRuku.setSupplierId(lvDaohuo.getSupplierId());
        lvRuku.setId(IdUtils.fastSimpleUUID());
        lvRukuMapper.insert(lvRuku);
        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(lvDaohuo.getId());
        List<LvDaohuoDetail> lvDaohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        BigDecimal totalMoney = BigDecimal.ZERO;
        for (LvDaohuoDetail daohuoDetail : lvDaohuoDetailList) {
            totalMoney = totalMoney.add(daohuoDetail.getTotalPrice());
            chukuByDaohuoDetail(lvRuku,daohuoDetail);
        }
        lvRuku.setMoney(totalMoney);
        lvRukuMapper.update(lvRuku);

        // 审核入库 到货 形成应付账款
        financeApService.addByStoreOut(lvRuku);

    }

    /**
     * 根据产品入库
     * @param
     * @return
     */
    public boolean rukuByDaohuo(GoodsOrderIn goodsOrderIn) {







        return true;
    }




}
